﻿@model ElectrosLtdApplication.Models.ManageReturnsModel
@{string randomNum = ViewBag.randomNum;}
@{int pId = ViewBag.pId;}
@{Guid oId = ViewBag.oId;}
@{List<SelectListItem> faultList = ViewBag.faultList;}

@{
    ViewBag.Title = "Electros Ltd | Report a Fault";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Report a Fault</h2>

<script type="text/javascript">
    function validateEmpty() {
        if ($("#faultDetails").val() == "" || ($("#faultCategory").val() == "")) {
            alert("Please choose a fault category and enter the fault details to report the fault!");
        }
        else {
            var randomNo = $('#randomNum').val();
            var fcat = $('#faultCategory option:selected').val();
            var oid = $('#orderId').val();
            var pid = $('#productId').val();
            var details = $('#faultDetails').val();
            var param = { randomNumber: randomNo, FaultCategory: fcat, OrderID: oid, ProductID: pid, FaultDetails: details };
            $.ajax({
                url: '@Url.Action("FaultDetails")',
                type: "POST",
                datatype: "json",
                data: param,
                error: function (xmlHttpRequest, errorText, thrownError) {
                    alert("An error has occurred.");
                },
                success: function (data) {
                    if (data != null) {
                        $("#faultDetails").val("");
                        $("#faultCategory").val("");
                        alert("Thank you for submitting a fault!");
                    }
                }
            });
        }
    }
</script>

@using (Html.BeginForm())
{
    <fieldset>
        <legend>Enter fault details</legend>

        <div style="visibility:hidden; display:inline-block">
            @Model.ProductID = @pId  
            @Html.HiddenFor(m => m.ProductID, new {id = "productId"})

            @Model.OrderID = @oId
            @Html.HiddenFor(m => m.OrderID, new {id = "orderId"})

            @Model.RandomNumber = @randomNum
            @Html.HiddenFor(m => m.RandomNumber, new {id = "randomNum"})
        </div>

        <div style="color:Red">
            @ViewBag.Message
        </div>

        <div class="editor-label">
            <b>@Html.LabelFor(model => model.FaultCategory, "Fault Category")</b>
        </div>

        <div class="editor-field">
            @Html.DropDownListFor(model => model.FaultCategory, faultList, "--Select Fault Category--", new { id = "faultCategory" })
            @Html.ValidationMessageFor(model => model.FaultCategory)
        </div>

        <div class="editor-label">
            <b>@Html.LabelFor(model => model.FaultDetails, "Fault Details")</b>
        </div>

        <div class="editor-field">
            @Html.TextAreaFor(model => model.FaultDetails, new { id = "faultDetails" })
            @Html.ValidationMessageFor(model => model.FaultDetails)
        </div>

        @*<div class="editor-label">
            <b>@Html.LabelFor(model => model.FaultStatus)</b>
        </div>

        <div class="editor-field">
            @Html.HiddenFor(model => model.FaultStatus)
        </div>*@

        @*<div>
            <b>@Html.LabelFor(model => model.Barcode)</b>
        </div>

        <div>
            <img src="@Url.Action("GetBarcode", new { rand =  randomNum})" alt="Barcode"  />
        </div>*@

        <p>
            @*<input type="submit" value="Submit Fault" name="FaultDetails"/>*@
            <button type="button" onclick="validateEmpty()">Submit Fault</button>
        </p>
    </fieldset>
}